home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Special 25 / AMIGAplus Sonderheft 25 (2000)(Falke)(DE)(Track 1 of 4)[!].iso / PublicDomain / Anwendungen / SerDate38.2 / SerDate.c < prev    next >
C/C++ Source or Header  |  2000-01-01  |  5KB  |  190 lines

  1. /*
  2.  * serdate 38.2
  3.  *
  4.  * Display current date and time using locale.library's format capabilities.
  5.  * This is NOT a "Date"-replacement !!!
  6.  *
  7.  * You may find this program useful for date stamps in logfiles, for example.
  8.  * I need it to show the time on a serial port LED-display, this may explain
  9.  * the strange name... ;-)
  10.  */
  11.  
  12. #include <pragmas/exec_pragmas.h>
  13. #include <pragmas/dos_pragmas.h>
  14. #include <pragmas/locale_pragmas.h>
  15. #include <stdlib.h>
  16.  
  17. #define BUFSIZE 256
  18.  
  19. /* Taken from the autodocs and slightly modified... */
  20. STRPTR helptext =
  21. "%a - abbreviated weekday name           %A - weekday name\n"
  22. "%b - abbreviated month name             %B - month name\n"
  23. "%c - same as %a %b %d %H:%M:%S %Y       %C - same as %a %b %e %T %Z %Y\n"
  24. "%d - day number with leading 0s         %D - same as %m/%d/%y\n"
  25. "%e - day number with leading spaces\n"
  26. "%h - abbreviated month name             %H - hour 24-hour style, leading 0s\n"
  27. "%I - hour, 12-hour style, leading 0s\n"
  28. "%j - julian date\n"
  29. "%m - month number with leading 0s       %M - number of minutes, leading 0s\n"
  30. "%n - insert a linefeed\n"
  31. "%p - AM or PM strings\n"
  32. "%q - hour using 24-hour style\n"
  33. "%Q - hour using 12-hour style\n"
  34. "%r - same as %I:%M:%S %p                %R - same as %H:%M\n"
  35. "%S - number of seconds, leadings 0s\n"
  36. "%t - insert a tab character             %T - same as %H:%M:%S\n"
  37. "%U - week number, Sunday first weekday\n"
  38. "%w - weekday number                     %W - week number, Monday first weekday\n"
  39. "%x - same as %m/%d/%y                   %X - same as %H:%M:%S\n"
  40. "%y - year, two digits, leading 0s       %Y - year, four digits, leading 0s\n";
  41.  
  42. /* BEWARE: The "__DATE__" and "__DATE2__" macro from MaxonDevelop V4 is not Y2K-ready !!! */
  43. const STRPTR versionstring = "$VER:serdate 38.2 (01.01.2000) "__TIME__;
  44.  
  45. struct Library *LocaleBase = NULL;
  46.  
  47. /* Function prototypes */
  48. ULONG print(register __a0 struct Hook *h, register __a1 APTR data, register __a2 APTR locale);
  49. void wbmain(APTR wbarg);
  50.  
  51. /* System data structures */
  52. struct DateStamp dateStamp =
  53. {
  54.     0L,    /* ds_Days */
  55.     0L,    /* ds_Minute */
  56.     0L        /* ds_Tick */
  57. };
  58.  
  59. struct Hook dateHook =
  60. {
  61.     {NULL, NULL},    /* h_MinNode */
  62.     NULL,                /* h_Entry */
  63.     NULL,                /* h_SubEntry */
  64.     NULL                /* h_Data */
  65. };
  66.  
  67. /* Private data structures */
  68. struct hookData
  69. {
  70.     UBYTE *string;
  71.     ULONG counter;
  72.     ULONG maxcount;
  73. };
  74.  
  75. struct hookData hdata =
  76. {
  77.     NULL,
  78.     0U,
  79.     BUFSIZE - 1
  80. };
  81.  
  82. /* Main function */
  83. void main(ULONG mode, APTR args)
  84. {
  85.     struct Locale *locale;
  86.     struct RDArgs *rda;
  87.     UBYTE buffer[BUFSIZE];
  88.     LONG arg[2] = {0L, 0L};
  89.     STRPTR dateformat = "%A %d-%b-%Y  %H:%M:%S%n";
  90.     
  91.     LocaleBase = OpenLibrary("locale.library", 38U);
  92.     if(!LocaleBase)
  93.     {
  94.         /* Write() is OS1.2-safe... :) */
  95.         Write(Output(), "This program needs OS2.1+ !\n", 28U);
  96.         exit(RETURN_FAIL);
  97.     }
  98.     
  99.     /* Format date function needs current locale */
  100.     locale = OpenLocale(NULL);
  101.     if(!locale)
  102.     {
  103.         PutStr("Error opening standard Locale\n");
  104.         CloseLibrary(LocaleBase);
  105.         exit(RETURN_FAIL);
  106.     }
  107.     
  108.     if(mode)
  109.     {
  110.         rda = ReadArgs("FORMAT,HELP/S", arg, NULL);
  111.         if(rda)
  112.         {
  113.             /* At first, answer call for help... */
  114.             if(arg[1])
  115.             {
  116.                 PutStr(helptext);
  117.                 Printf("\nBuilt-in format is \"%s\": ", dateformat);
  118.             }
  119.             
  120.             /* Change date format, only if no help requested */
  121.             if(arg[0] && (!arg[1]))
  122.             {
  123.                 dateformat = (STRPTR) arg[0];
  124.             }
  125.         }
  126.     }
  127.     
  128.     /* Initialize Hook-related data structures */
  129.     hdata.string = buffer;
  130.     
  131.     dateHook.h_Entry = (HOOKFUNC) print;
  132.     dateHook.h_Data = &hdata;
  133.     
  134.     /* At last: format date string */
  135.     FormatDate(locale, dateformat, DateStamp(&dateStamp), &dateHook);
  136.     
  137.     /* Release all (used) resources */
  138.     if(mode)
  139.     {
  140.         FreeArgs(rda);
  141.     }
  142.     
  143.     CloseLocale(locale);
  144.     CloseLibrary(LocaleBase);
  145.     
  146.     /* Finally output date string to stdout */
  147.     PutStr(buffer);
  148.     
  149.     exit(RETURN_OK);
  150. }
  151.  
  152. /* Hook-function to handle parsed chars (Maxon registerized format) */
  153. ULONG print(register __a0 struct Hook *h, register __a1 APTR data, register __a2 APTR locale)
  154. {
  155.     register UBYTE *tmp, c;
  156.     register ULONG cnt;
  157.     
  158.     /* Data must be copied from adress register to someplace else in order to be accessed properly... */
  159.     c = (UBYTE) data;
  160.     
  161.     /* Only do structure offset calculations once - use local variables ! */
  162.     tmp = ((struct hookData *)h->h_Data)->string;
  163.     cnt = ((struct hookData *)h->h_Data)->counter;
  164.     
  165.     /* Check for buffer overflow */
  166.     if(cnt < (BUFSIZE - 1))
  167.     {
  168.         tmp[cnt] = c;
  169.     }
  170.     else
  171.     {
  172.         /* If buffer is full, just set final null-byte and exit */
  173.         tmp[BUFSIZE - 1] = 0;
  174.         return(0U);
  175.     }
  176.     
  177.     /* This could only be optimized by a pointer reference, which means overhead... */
  178.     ((struct hookData *)h->h_Data)->counter = cnt + 1;
  179.     
  180.     /* Just a dummy... */
  181.     return((ULONG) data);
  182. }
  183.  
  184. /* Workbench functionality not yet implemented */
  185. void wbmain(APTR wbarg)
  186. {
  187.     main(0, wbarg);
  188. }
  189.  
  190.